package com.study.userserviceprovider.rxJava;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func0;

public class RxJavaDemo {

    private static String[] datas = new String[]{"登录","上课","听讲","答疑"};

    public static void main(String[] args) {
        final Action0 onComplated = new Action0() {
            @Override
            public void call() {
                System.out.println("on complated");
            }
        };


        Action1<String> onNextAction = new Action1<String>() {
            // onNext()
            @Override
            public void call(String s) {
                System.out.println("下次执行的为"+s);;
            }
        };

        Action1<Throwable> onErrorAction = new Action1<Throwable>() {
            // onError()
            @Override
            public void call(Throwable throwable) {
                // Error handling
            }
        };



        Observable<String> observable = Observable.defer(new Func0<Observable<String>>() {
            @Override
            public Observable<String> call() {
                Observable<String> observer = Observable.from(datas);
                //observer.subscribe(onNextAction,onErrorAction,onComplated);
                return observer.doOnCompleted(onComplated);
            }
        });

        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onStart() {
                System.out.println("subscriber开始");;
            }

            @Override
            public void onCompleted() {
                System.out.println("subscriber: complated");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("subscriber: error");
            }

            @Override
            public void onNext(String s) {
               System.out.println("subscriber next: "+s);
            }
        });





      /*  Observer<String> observer = new Observer<String>() {
            @Override
            public void onCompleted() {
                System.out.println("observer: complated");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("observer: error");
            }

            @Override
            public void onNext(String s) {
                System.out.println("observer next: "+s);
            }
        };*/



       // observable.subscribe(observer);

    }

}
